//
//  TodayBusiViewController.m
//  ShangAPP
//
//  Created by macpro on 16/1/22.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "TodayBusiViewController.h"
#import "TodayBusiTableViewCell.h"
#import "MyTools.h"
#import <MJRefresh.h>

@interface TodayBusiViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation TodayBusiViewController{
    UITableView   * _myTab;
    NSArray       * _array;
    NSDictionary  * _dic;
    NSDictionary  * _dict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"有效订单";
//    [self.navigationController.navigationBar setTitleTextAttributes:
//     @{NSFontAttributeName:[UIFont systemFontOfSize:20],
//       NSForegroundColorAttributeName:UIColorFromRGB(0x444444)}];
//    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
//    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"刷新"style:UIBarButtonItemStylePlain target:self action:@selector(shuaxin)];
//    [self.navigationItem.rightBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.view.backgroundColor = UIColorFromRGB(0XEFEFF1);
    
    [self requsetData];
    
//    [self viewCreateUI];

}
- (void)shuaxin
{
    [self requsetData];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
- (void)requsetData
{
    NSString *url = @"http://www.xsy777.com/app/b_send/dayorderlist.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{
                           @"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
//        NSLog(@"%d",[[responseObject objectForKey:@"status"] intValue]);
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            
            //_array =[[NSArray alloc]initWithArray:[responseObject objectForKey:@"data"]];
            _dic =[[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"]];
            
        }
        
        [_myTab reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
        
    }];
    
    
    NSString *url1 = @"http://www.xsy777.com/app/b_send/ordercount.html";
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url1 parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            
            
            _dict =[[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"]];
            
        }
        [self viewCreateUI];
        [_myTab reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
        
    }];
}


- (void)viewCreateUI{
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    headview.backgroundColor = UIColorFromRGB(0xefeff1);
    [self.view addSubview:headview];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    view1.backgroundColor = UIColorFromRGB(0xFCDE28);
    [headview addSubview:view1];
    

    UILabel *orderlab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, WIDTH, 40)];
    orderlab.text = @"今日营业额 (元)";
    orderlab.textAlignment =NSTextAlignmentCenter;
    orderlab.textColor = UIColorFromRGB(0x333333);
    orderlab.font = [UIFont systemFontOfSize:17];
    orderlab.textAlignment = NSTextAlignmentCenter;
    [view1 addSubview:orderlab];
    
    UILabel *numlab = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, WIDTH, 40)];
//    NSString * num =[NSString stringWithFormat:@"￥%d",[[_dicobjectForKey:@"price_count"]intValue]];
//    numlab.text = num;
    if ([_dict objectForKey:@"price_count"] == nil || [_dict objectForKey:@"price_count"] == [NSNull null])
    {
        numlab.text = @"￥0";
    }
    if ([[_dict objectForKey:@"price_count"] isKindOfClass:[NSString class]])
    {
        numlab.text =[NSString stringWithFormat:@"￥%@",[_dict objectForKey:@"price_count"]];
    }
    if ([[_dict objectForKey:@"price_count"] isKindOfClass:[NSNumber class]])
    {
        NSNumber * numm =[_dict objectForKey:@"price_count"];
        numlab.text =[NSString stringWithFormat:@"￥%@",numm];
    }
    numlab.textAlignment =NSTextAlignmentCenter;
    numlab.textColor = UIColorFromRGB(0xff7700);
    numlab.font = [UIFont systemFontOfSize:23];
    numlab.textAlignment = NSTextAlignmentCenter;
    [view1 addSubview:numlab];
    
    
    UILabel * propertyLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 120, WIDTH/2, 40)];
    propertyLab.text =@"总金额 (元)";
    propertyLab.textAlignment =NSTextAlignmentCenter;
    propertyLab.font =[UIFont systemFontOfSize:12];
    [view1 addSubview:propertyLab];
    
    UILabel * propertyNum =[[UILabel alloc]initWithFrame:CGRectMake(0, 160, WIDTH/2, 40)];
//    propertyNum.text =[_dict objectForKey:@"count_price_zuizhong"];
    if ([_dict objectForKey:@"count_price_zuizhong"] == nil || [_dict objectForKey:@"count_price_zuizhong"] == [NSNull null])
    {
        propertyNum.text = @"0";
    }
    if ([[_dict objectForKey:@"count_price_zuizhong"] isKindOfClass:[NSString class]])
    {
        propertyNum.text =[_dict objectForKey:@"count_price_zuizhong"];
    }
    if ([[_dict objectForKey:@"count_price_zuizhong"] isKindOfClass:[NSNumber class]])
    {
        NSNumber * numm =[_dict objectForKey:@"count_price_zuizhong"];
        propertyNum.text =[NSString stringWithFormat:@"%@",numm];
    }
    propertyNum.textColor =[UIColor redColor];
    propertyNum.textAlignment =NSTextAlignmentCenter;
    propertyNum.font =[UIFont systemFontOfSize:16];
    [view1 addSubview:propertyNum];
    
    UILabel * profitLab =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, 120, WIDTH/2, 40)];
    profitLab.text =@"累计积分 (元)";
    profitLab.textAlignment =NSTextAlignmentCenter;
    profitLab.font=[UIFont systemFontOfSize:12];
    [view1 addSubview:profitLab];
    
    UILabel * profitNum =[[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, 160, WIDTH/2, 40)];
    if ([_dict objectForKey:@"score2"] == nil || [_dict objectForKey:@"score2"] == [NSNull null])
    {
        profitNum.text = @"0";
    }
    if ([[_dict objectForKey:@"score2"] isKindOfClass:[NSString class]])
    {
        profitNum.text =[_dict objectForKey:@"score2"];
    }
    if ([[_dict objectForKey:@"score2"] isKindOfClass:[NSNumber class]])
    {
        NSNumber * numm =[_dict objectForKey:@"score2"];
        profitNum.text =[NSString stringWithFormat:@"%@",numm];
    }
    profitNum.textAlignment =NSTextAlignmentCenter;
    profitNum.textColor =[UIColor redColor];
    profitNum.font =[UIFont systemFontOfSize:16];
    [view1 addSubview:profitNum];
    
    
    UIImageView * iviewBack1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 130, WIDTH, 70)];
    iviewBack1.image =[UIImage imageNamed:@"w3"];
    [view1 addSubview:iviewBack1];
    
    UIImageView * iviewBack2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 130, WIDTH, 70)];
    iviewBack2.image =[UIImage imageNamed:@"w2"];
    [view1 addSubview:iviewBack2];
    
    
    
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 200, WIDTH, 50)];
    view2.backgroundColor = UIColorFromRGB(0xFDED8A);
    [headview addSubview:view2];
    
    
    UILabel *numLab = [[UILabel alloc]initWithFrame:CGRectMake(5 * MYWIDTH, 10, 140 * MYWIDTH, 30)];
    numLab.text = @"订单号";
    numLab.textColor = UIColorFromRGB(0x4e4f56);
    numLab.font = [UIFont systemFontOfSize:14];
    numLab.textAlignment = NSTextAlignmentCenter;
    [view2 addSubview:numLab];
    UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(145 * MYWIDTH, 10, 90 * MYWIDTH, 30)];
    timeLab.text = @"下单时间";
    timeLab.textColor = UIColorFromRGB(0x4e4f56);
    timeLab.font = [UIFont systemFontOfSize:14];
    timeLab.textAlignment = NSTextAlignmentCenter;
    [view2 addSubview:timeLab];
    UILabel *stateLab = [[UILabel alloc]initWithFrame:CGRectMake(245 * MYWIDTH, 10, 60 * MYWIDTH, 30)];
    stateLab.text = @"状态";
    stateLab.textColor = UIColorFromRGB(0x4e4f56);
    stateLab.font = [UIFont systemFontOfSize:14];
    stateLab.textAlignment = NSTextAlignmentCenter;
    [view2 addSubview:stateLab];
    UILabel *moneyLab = [[UILabel alloc]initWithFrame:CGRectMake(310 * MYWIDTH, 10, WIDTH - 315 * MYWIDTH, 30)];
    moneyLab.text = @"金额";
    moneyLab.textColor = UIColorFromRGB(0x4e4f56);
    moneyLab.font = [UIFont systemFontOfSize:14];
    moneyLab.textAlignment = NSTextAlignmentCenter;
    [view2 addSubview:moneyLab];
    
    
    _myTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-67 )];
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
    _myTab.tableHeaderView = headview;
    [_myTab addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(shuaxin)];
    _myTab.delegate = self;
    _myTab.dataSource = self;
    _myTab.backgroundColor = UIColorFromRGB(0XEFEFF1);
    [self.view addSubview:_myTab];
    
}
#pragma mark tableview代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%lu",(unsigned long)_dic.count);
    if (_dic.count >= 3) {
        return [[_dic objectForKey:@"count"]integerValue];
    }else{
        return 0;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    TodayBusiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[TodayBusiTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSArray * list =[[NSArray alloc]initWithArray:[_dic objectForKey:@"list"]];
    if ([list[indexPath.row][@"ordermum"] isEqual:[NSNull null]]) {
        cell.numberLabel.text =@"";
    }else{
//        cell.numberLabel.text =list[indexPath.row][@"ordermum"];
        cell.numberLabel.text =[NSString stringWithFormat:@"%@",list[indexPath.row][@"ordermum"]];
    }
    if ([list[indexPath.row][@"create_time"] isEqual:[NSNull null]]) {
        cell.timeLabel.text =@"";
    }else{
//        cell.timeLabel.text   =list[indexPath.row][@"create_time"];
        cell.timeLabel.text =[NSString stringWithFormat:@"%@",list[indexPath.row][@"create_time"]];
    }
    if ([list[indexPath.row][@"count_price"] isEqual:[NSNull null]]) {
        cell.moneyLabel.text =@"";
    }else{
//        cell.moneyLabel.text  =list[indexPath.row][@"count_price"];
        cell.moneyLabel.text =[NSString stringWithFormat:@"%@",list[indexPath.row][@"count_price"]];
    }
//    cell.numberLabel.text =list[indexPath.row][@"ordermum"];
//    cell.timeLabel.text   =list[indexPath.row][@"create_time"];
//    cell.moneyLabel.text  =list[indexPath.row][@"count_price"];
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
    [super viewWillAppear:animated];
}

@end
